/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.message.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.message.entity.StoreEmployeeTask;
import com.chlitina.store.modules.message.service.StoreEmployeeTaskService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 员工任务单Controller
 * @author lvbo
 * @version 2013-11-09
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/message/storeEmployeeTask")
public class StoreEmployeeTaskController extends BaseController {

	@Autowired
	private StoreEmployeeTaskService storeEmployeeTaskService;
	
	@ModelAttribute
	public StoreEmployeeTask get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeEmployeeTaskService.get(id);
		}else{
			return new StoreEmployeeTask();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(StoreEmployeeTask storeEmployeeTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		if (store != null){
			storeEmployeeTask.setStore(store);
		}
		String currentSelectId = request.getParameter("currentSelectId");
		String status = request.getParameter("status");
		String finished = request.getParameter("finished");
		String employeeName = request.getParameter("employeeName");
		String notifyDate = request.getParameter("notifyDate");
		Page<StoreEmployeeTask> page = new Page<StoreEmployeeTask>(request, response);
		if (status != null || finished != null){
			page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
			List<StoreEmployeeTask> list = storeEmployeeTaskService.find(storeEmployeeTask, status, finished, employeeName, notifyDate); 
			page.updatePage(list);
		}
		model.addAttribute("notifyDate", notifyDate);
        model.addAttribute("status", status);
        model.addAttribute("finished", finished);
        model.addAttribute("employeeName", employeeName);
        model.addAttribute("currentSelectId", currentSelectId);
        model.addAttribute("page", page);
		return "modules/message/storeEmployeeTaskList";
	}

	@RequestMapping(value = "detail")
	public String form(StoreEmployeeTask storeEmployeeTask, Model model) {
		model.addAttribute("storeEmployeeTask", storeEmployeeTask);
		int status = storeEmployeeTask.getViewed();
		if (status == StoreEmployeeTask.STATUS_NOT_VIEWED) {
			storeEmployeeTaskService.changeStatus(storeEmployeeTask);
		}
		return "modules/message/storeEmployeeTaskDetail";
	}
	
	@RequestMapping(value = "finished")
	public String finished(StoreEmployeeTask storeEmployeeTask, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, storeEmployeeTask)){
			return form(storeEmployeeTask, model);
		}
		String currentSelectId = request.getParameter("currentSelectId");
		String pageNo = request.getParameter("pageNo");
		String status = request.getParameter("status");
		String finished = request.getParameter("finished");
		String employeeName = request.getParameter("employeeName");
		String notifyDate = request.getParameter("notifyDate");
		storeEmployeeTaskService.changeToFinished(storeEmployeeTask);
		addMessage(redirectAttributes, "状态修改成功");
		return "redirect:"+Global.ADMIN_PATH+"/message/storeEmployeeTask/?pageNo=" +pageNo + "&currentSelectId=" +currentSelectId+ "&notifyDate=" +notifyDate+ "&status=" +status+ "&finished=" +finished+ "&employeeName=" +employeeName;
	}
	@RequestMapping(value = "changeStatus")
	public String changeStatus(StoreEmployeeTask storeEmployeeTask, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, storeEmployeeTask)){
			return form(storeEmployeeTask, model);
		}
		storeEmployeeTaskService.changeStatus(storeEmployeeTask);
		addMessage(redirectAttributes, "状态修改成功");
		return "redirect:"+Global.ADMIN_PATH+"/message/storeEmployeeTask/";
	}

}
